﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Workflow.Activities;
using System.Workflow.Runtime;
using Common.Logging;
using MailCrawler.Workflow;

namespace MailCrawler
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window,IReturnMessaging
    {
        private ILog _log = LogManager.GetCurrentClassLogger();
        
        public MainWindow()
        {
            InitializeComponent();
            this.Messages = new ObservableCollection<Message>();
        }

        private WorkflowRuntime _workflowRuntimereg = new WorkflowRuntime();
        private WorkflowInstance _instance;
        private void Start_Click(object sender, RoutedEventArgs e)
        {
            if (!_workflowRuntimereg.IsStarted)
            {
                if (_instance == null)
                {
                    _instance = _workflowRuntimereg.CreateWorkflow(typeof(Workflow.CheckerWorkflow));
                    
                    ExternalDataExchangeService dataservice = new ExternalDataExchangeService();
                    _workflowRuntimereg.AddService(dataservice);
                    dataservice.AddService(this);
                    _instance.Start();
                    _log.Debug("Process Started");
                }
                else
                {
                    _workflowRuntimereg.StartRuntime();
                    _instance.Resume();
                }

                Action fun = delegate
                {
                    Start.Content = "Pause";
                };
                Dispatcher.Invoke(fun);
            }
            else
            {
                _instance.Suspend("");
                _workflowRuntimereg.StopRuntime();
                Action fun = delegate
                {
                    Start.Content = "Resume";
                };
                Dispatcher.Invoke(fun);
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            ClickedCount.DataContext = 0.ToString();
            MessagesChecked.DataContext = 0.ToString();
            FoundedCount.DataContext = 0.ToString();
            Rules.DataContext = MailCrawler.Workflow.RuleFetcher.Rules;
            Headers.DataContext = HeaderFetcher.HeaderFiles;
            EMails.DataContext = MailCrawler.Workflow.EmailFetcher.EMails;
            Proxies.DataContext = MailCrawler.Workflow.ProxyFetcher.Proxies;
            MessageList.DataContext = Messages;
        }
        public ObservableCollection <Message> Messages{ get; set;}

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            MailCrawler.Workflow.RuleFetcher.Save();
            MailCrawler.Workflow.EmailFetcher.Save();
            MailCrawler.Workflow.ProxyFetcher.Save();
        }

        private void Headers_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            var v = (FileInfo)((ComboBox)sender).SelectedItem;
            HeaderFetcher.Select(v);
        }

        #region IReturnMessaging Members
        public int Checked { get; set; }
        public void CheckedOneMail()
        {
            Action fun = delegate
            {
                Checked += 1;
                MessagesChecked.DataContext = Checked;

                Messages.Insert(0, new Message() { Text = "Checked EMail message" });
            };
            Dispatcher.Invoke(fun);
        }
        public int Founded { get; set; }
        public void FoundedLinks(int count)
        {
            Action fun = delegate
            {
                Founded+=count;
                FoundedCount.DataContext = Founded;

                Messages.Insert(0, new Message() { Text = "Founded " + count + " links" });
            };
            Dispatcher.Invoke(fun);
        }
        public int Clicked { get; set; }
        public void ClickedOneLink(string link)
        {
            Action fun = delegate
            {
                Clicked++;
                ClickedCount.DataContext = Clicked;

                Messages.Insert(0, new Message() { Text = "Clicked on " + link });
            };
            Dispatcher.Invoke(fun);
        }
        public void Error(Exception ex)
        {
            Action fun = delegate
                             {
                                 Messages.Insert(0,new Message() {Text = ex.Message});
                             };

            Dispatcher.Invoke(fun);
        }
        public void Info(string text)
        {
            Action fun = delegate
                             {
                                 Messages.Insert(0, new Message() { Text = text });
                             };
            Dispatcher.Invoke(fun);
        }
        #endregion

        private void Hyperlink_RequestNavigate(object sender, System.Windows.Navigation.RequestNavigateEventArgs e)
        {
            Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri));
            e.Handled = true;
        }

        private void Hyperlink_RequestNavigate_1(object sender, System.Windows.Navigation.RequestNavigateEventArgs e)
        {
            Process.Start(new ProcessStartInfo(e.Uri.ToString()));
            e.Handled = true;
        }
    }

    public class Message
    {
        public string Text { get; set; }
    }
}
